package com.canxi.user.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.canxi.common.annotation.Log;
import com.canxi.common.annotation.RepeatSubmit;
import com.canxi.common.core.controller.BaseController;
import com.canxi.common.core.domain.PageQuery;
import com.canxi.common.core.domain.R;
import com.canxi.common.core.page.TableDataInfo;
import com.canxi.common.core.validate.AddGroup;
import com.canxi.common.core.validate.EditGroup;
import com.canxi.common.enums.BusinessType;
import com.canxi.common.utils.poi.ExcelUtil;
import com.canxi.user.domain.bo.CxConfigBo;
import com.canxi.user.domain.vo.CxConfigVo;
import com.canxi.user.service.IConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 微信配制
 *
 * @author ruoyi
 * @date 2022-10-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/user/userConfig")
public class ConfigController extends BaseController {

    private final IConfigService iConfigService;

    /**
     * 查询微信配制列表
     */
    @SaCheckPermission("user:userConfig:list")
    @GetMapping("/list")
    public TableDataInfo<CxConfigVo> list(CxConfigBo bo, PageQuery pageQuery) {
        return iConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出微信配制列表
     */
    @SaCheckPermission("user:userConfig:export")
    @Log(title = "微信配制", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CxConfigBo bo, HttpServletResponse response) {
        List<CxConfigVo> list = iConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "微信配制", CxConfigVo.class, response);
    }

    /**
     * 获取微信配制详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("user:userConfig:query")
    @GetMapping("/{id}")
    public R<CxConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iConfigService.queryById(id));
    }

    /**
     * 新增微信配制
     */
    @SaCheckPermission("user:userConfig:add")
    @Log(title = "微信配制", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CxConfigBo bo) {
        return toAjax(iConfigService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改微信配制
     */
    @SaCheckPermission("user:userConfig:edit")
    @Log(title = "微信配制", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CxConfigBo bo) {
        return toAjax(iConfigService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除微信配制
     *
     * @param ids 主键串
     */
    @SaCheckPermission("user:userConfig:remove")
    @Log(title = "微信配制", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iConfigService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
